//
//  OtherLoginViewController.m
//  ToYourHeartNews
//
//  Created by XJing on 16/2/24.
//  Copyright © 2016年 www.lanou3g.com 蓝鸥. All rights reserved.
//

#import "OtherLoginViewController.h"
#import "UMSocial.h"//友盟分享
#import "AfNetWorkingHandler.h"
#import <UIImageView+WebCache.h>
#import "MyselfViewController.h"
#import "MessageViewController.h"
#import "PhoneLoginViewController.h"

#import "RegisterViewController.h"

#import <SMS_SDK/SMSSDK.h>

@interface OtherLoginViewController ()

@end

@implementation OtherLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"登录";
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height / 4 * 3)];
    [img setImage:[UIImage imageNamed:@"phone.png"]];
    [self.view addSubview:img];
    [img release];
    //手机号登录
    UIButton *phone = [UIButton buttonWithType:UIButtonTypeCustom];
    phone.frame = CGRectMake(self.view.frame.size.width / 5, self.view.frame.size.height / 10 * 6, self.view.frame.size.width / 5 * 3, 40);
    CGRect f = phone.frame;
    [phone addTarget:self action:@selector(phoneAction) forControlEvents:UIControlEventTouchUpInside];
    phone.layer.cornerRadius = 10;
    phone.clipsToBounds = YES;
    [phone setBackgroundColor:[UIColor redColor]];
    [phone setTitle:@"手机号登录" forState:UIControlStateNormal];
    [self.view addSubview:phone];
    //短信验证
    UIButton *checkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    f.origin.y += phone.frame.size.height + 20;
    checkBtn.frame = f;
    checkBtn.layer.cornerRadius = 10;
    checkBtn.clipsToBounds = YES;
    [checkBtn setBackgroundColor:[UIColor redColor]];
    [checkBtn setTitle:@"立即注册" forState:UIControlStateNormal];
    [checkBtn addTarget:self action:@selector(checkAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:checkBtn];
    //其他方式登录
    UILabel *other = [[UILabel alloc]initWithFrame:CGRectMake(10, self.view.frame.size.height / 5 * 4, self.view.frame.size.width - 20, 30)];
    other.textAlignment = NSTextAlignmentCenter;
    other.text = @"--------- 其他方式登录---------";
    other.textColor = [UIColor grayColor];
    [self.view addSubview:other];
    [other release];
    //新浪微博登陆
    UIButton *sinalog = [UIButton buttonWithType:UIButtonTypeCustom];
    sinalog.frame = CGRectMake(self.view.frame.size.width / 7, other.frame.origin.y + other.frame.size.height + 20, self.view.frame.size.height / 11, self.view.frame.size.height / 11);
    sinalog.layer.cornerRadius = self.view.frame.size.height / 22;
    sinalog.clipsToBounds = YES;
    [sinalog setBackgroundImage:[UIImage imageNamed:@"sina.png"] forState:UIControlStateNormal];
    [sinalog addTarget:self action:@selector(sinalogAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:sinalog];
    //腾讯
    UIButton *tentlog = [UIButton buttonWithType:UIButtonTypeCustom];
    tentlog.frame = CGRectMake(self.view.frame.size.width / 7 * 3, other.frame.origin.y + other.frame.size.height + 20, self.view.frame.size.height / 11, self.view.frame.size.height / 11);
    tentlog.layer.cornerRadius = self.view.frame.size.height / 22;
    tentlog.clipsToBounds = YES;
    [tentlog setBackgroundImage:[UIImage imageNamed:@"tencent"] forState:UIControlStateNormal];
    [tentlog addTarget:self action:@selector(tentlogAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:tentlog];
    //人人
    UIButton *peolog = [UIButton buttonWithType:UIButtonTypeCustom];
    peolog.frame = CGRectMake(self.view.frame.size.width / 7 * 5, other.frame.origin.y + other.frame.size.height + 20, self.view.frame.size.height / 11, self.view.frame.size.height / 11);
    peolog.layer.cornerRadius = self.view.frame.size.height / 22;
    peolog.clipsToBounds = YES;
    [peolog setBackgroundImage:[UIImage imageNamed:@"peopeo"] forState:UIControlStateNormal];
    [peolog addTarget:self action:@selector(peologAction) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:peolog];
    
    //短信认证
    MFMessageComposeViewController *controller = [[MFMessageComposeViewController alloc]init];
    controller.messageComposeDelegate = self;
    [controller release];
    
}
#pragma mark - 点击屏幕收起键盘
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}
#pragma mark - 短信验证
-(void)checkAction
{
    MessageViewController *messageVC = [[MessageViewController alloc]init];
    [self.navigationController pushViewController:messageVC animated:YES];
    [messageVC release];

}
#pragma mark - 手机号登录
-(void)phoneAction
{
    PhoneLoginViewController *phoneLoginVC = [[PhoneLoginViewController alloc]init];
    [self.navigationController pushViewController:phoneLoginVC animated:YES];
    [phoneLoginVC release];
    
}
#pragma mark - 人人
-(void)peologAction
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToRenren];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],NO,^(UMSocialResponseEntity *response){
        
        //          获取用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"OtherLogin"];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToRenren];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"register"];
            [[NSUserDefaults standardUserDefaults]setObject:snsAccount.userName forKey:@"name"];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:snsAccount.iconURL forKey:@"img"];
            //登陆成功后跳回我的页面
            [self.navigationController popViewControllerAnimated:YES];
            
        }});

}
#pragma mark - 腾讯登陆
-(void)tentlogAction
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToTencent];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],NO,^(UMSocialResponseEntity *response){
        
        //          获取用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"OtherLogin"];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToTencent];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"register"];
            [[NSUserDefaults standardUserDefaults]setObject:snsAccount.userName forKey:@"name"];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:snsAccount.iconURL forKey:@"img"];
            //登陆成功后跳回我的页面
            [self.navigationController popViewControllerAnimated:YES];
            
        }});

}
#pragma mark - 微博登录
-(void)sinalogAction
{
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],NO,^(UMSocialResponseEntity *response){
        
        //          获取微博用户名、uid、token等
        
        if (response.responseCode == UMSResponseCodeSuccess) {
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"OtherLogin"];
            UMSocialAccountEntity *snsAccount = [[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"register"];
            [[NSUserDefaults standardUserDefaults]setObject:snsAccount.userName forKey:@"name"];
            NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
            [userDefaults setObject:snsAccount.iconURL forKey:@"img"];
            //登陆成功后跳回我的页面
            [self.navigationController popViewControllerAnimated:YES];
            
        }});
   
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
